package com.example.health_life.service;

import com.example.health_life.dao.MyFavorite;
import com.example.health_life.dao.MyFavoriteRepository;
import com.example.health_life.dao.ArticleRepository;
import com.example.health_life.dao.Article;
import com.example.health_life.dao.New;
import com.example.health_life.dao.NewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyFavoriteServiceImpl implements MyFavoriteService{

    @Autowired
    private MyFavoriteRepository myFavoriteRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private NewRepository newRepository;

    @Override
    public List<MyFavorite> getMyfavoriteList(Integer status) {
        List<MyFavorite> myfavorites = null;
        if(status ==0 ){
             myfavorites = myFavoriteRepository.findAll();
        }
        if(status ==1){
             myfavorites = myFavoriteRepository.findByStatus((1));
        }
        if(status ==-1){
             myfavorites = myFavoriteRepository.findByStatus((-1));
        }
        return myfavorites;
    }

    @Override
    public MyFavorite getMyfavoriteById(Integer id) {
        return myFavoriteRepository.findById(id).get();
    }

    @Override
    public List<MyFavorite> getMyfavoriteByUserId(Integer userId, Integer status) {
        return myFavoriteRepository.findByUserIdAndStatus(userId,status);
    }

    @Override
    public List<MyFavorite> getMyfavoriteByTypeId(Byte type,Integer status) {
        return myFavoriteRepository.findByTypeAndStatus(type, status);
    }

    @Override
    public Integer deleteMyfavorite(Integer id) {
        //查找记录是否存在
        MyFavorite myFavorite = myFavoriteRepository.findById(id).orElseThrow(()->new IllegalStateException("记录不存在"));
        
        // 检查是否存在冲突记录
        byte newStatus = (byte)(-1 * myFavorite.getStatus());
        List<MyFavorite> existingFavorites = myFavoriteRepository.findByUserIdAndStatus(myFavorite.getUserId(), newStatus);
        for (MyFavorite existing : existingFavorites) {
            if (existing.getType().equals(myFavorite.getType()) && 
                existing.getTypeId().equals(myFavorite.getTypeId())) {
                // 如果存在冲突记录，先删除冲突记录
                myFavoriteRepository.delete(existing);
            }
        }
        
        //将状态改为相反数
        myFavorite.setStatus(newStatus);
        myFavoriteRepository.save(myFavorite);
        return id;
    }

    @Override
    public Integer addMyfavorite(MyFavorite myfavorite) {
        // 检查是否已存在相同的收藏记录
        List<MyFavorite> existingFavorites = myFavoriteRepository.findByUserIdAndStatus(myfavorite.getUserId(), myfavorite.getStatus());
        for (MyFavorite existing : existingFavorites) {
            if (existing.getType().equals(myfavorite.getType()) && 
                existing.getTypeId().equals(myfavorite.getTypeId())) {
                // 如果找到相同的记录，直接返回其ID
                return existing.getId();
            }
        }
        
        // 设置默认值
        myfavorite.setStatus((byte) 1);
        myfavorite.setAddTime(java.time.Instant.now());
        return myFavoriteRepository.save(myfavorite).getId();
    }

    @Override
    public Integer increaseFavoriteCount(Integer articleId) {
        Article article = articleRepository.findById(articleId).orElseThrow(() -> new IllegalStateException("文章不存在"));
        article.setFavorite(article.getFavorite() + 1);
        articleRepository.save(article);
        return article.getFavorite();
    }

    @Override
    public Integer decreaseFavoriteCount(Integer articleId) {
        Article article = articleRepository.findById(articleId).orElseThrow(() -> new IllegalStateException("文章不存在"));
        int count = article.getFavorite() > 0 ? article.getFavorite() - 1 : 0;
        article.setFavorite(count);
        articleRepository.save(article);
        return article.getFavorite();
    }

    @Override
    public Integer increaseFavoriteCountForNews(Integer newsId) {
        New news = newRepository.findById(newsId).orElseThrow(() -> new IllegalStateException("新闻不存在"));
        news.setFavorite(news.getFavorite() + 1);
        newRepository.save(news);
        return news.getFavorite();
    }

    @Override
    public Integer decreaseFavoriteCountForNews(Integer newsId) {
        New news = newRepository.findById(newsId).orElseThrow(() -> new IllegalStateException("新闻不存在"));
        int count = news.getFavorite() > 0 ? news.getFavorite() - 1 : 0;
        news.setFavorite(count);
        newRepository.save(news);
        return news.getFavorite();
    }
}
